﻿using System.Security.Claims;
using System.Threading.Tasks;
using AutoMapper;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using YYTea.MOdel.Dto;
using YYTea.MOdel.Model;
using YYTea.Services.IServices;
using YYTea.Services.VModel;

namespace YYTea.Miljy.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class ShoppingCarControllers : ControllerBase
    {
        private readonly IHttpContextAccessor _httpContextAccessor;
        private readonly IShoppingCar _shoppingCar;
        public ShoppingCarControllers(IHttpContextAccessor httpContextAccessor, IShoppingCar shoppingCar)
        {
            _httpContextAccessor = httpContextAccessor;
            _shoppingCar = shoppingCar;
        }
        [HttpGet]
        [Authorize(AuthenticationSchemes = "Bearer")]
        public async Task<IActionResult> GetShoppingCar()
        {
            //获取当前用户
            var userId = _httpContextAccessor.HttpContext.User.FindFirst(ClaimTypes.NameIdentifier).Value;
            //使用用户Id获取购物车
            var shop = await _shoppingCar.GetShoppingCarById(userId);

            return Ok(shop);
        }
        [Authorize(AuthenticationSchemes = "Bearer")]
        [AllowAnonymous]
        [HttpPost]
        public async Task<IActionResult> AddShoppingCarToProduct(ShoppingCarVModel shopVM)
        {
            //获取当前用户
            var userId = _httpContextAccessor.HttpContext.User.FindFirst(ClaimTypes.NameIdentifier).Value;

            shopVM.UserId = userId;
            //将物品添加到购物车
            //var shop = await _shoppingCar.GetShoppingCarById(userId);
            var shop = await _shoppingCar.AddProducrToShop(shopVM);
            return Ok(shop);
        }
        //[HttpPost]
        //public async Task<IActionResult> AddShoppingCarPro([FromBody] ShoppingCarVModel shopVM)
        //{
        //    //获取当前用户
        //    var userId = _httpContextAccessor.HttpContext.User.FindFirst(ClaimTypes.NameIdentifier).Value;
        //    //使用用户Id获取购物车
        //    var shop = await _shoppingCar.GetShoppingCarById(userId);
        //   //创建订单
        //   var order = await _shoppingCar.GetShoppingCarById()



        //}
    }
}
